<?php
class PNUserTopicsArray extends PNObjectExArray {
  function PNUserTopicsArray($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'innoforum_topics';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';
    $this->_objGroup      = 'tpc_id';
    $this->_objColumnExArray = array( 'is_new'      => "IF ( DATEDIFF(DATE(tpc_cr_date),DATE(NOW())) <1 
                                                             AND
                                                             DATEDIFF(DATE(tpc_cr_date),DATE(NOW())) >-3,
                                                            'T','F')");
/*
    $this->_objColumnExArray = array( );
*/
    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_type',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('type_name'),
                            'compare_field_table' =>  'type_id',
                            'compare_field_join'  =>  'id');
/*
    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_category',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('category_name'),
                            'compare_field_table' =>  'category_id',
                            'compare_field_join'  =>  'id');
*/
    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_subcategory',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('subcategory_name'),
                            'compare_field_table' =>  'subcategory_id',
                            'compare_field_join'  =>  'id');
/*

    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_posts',
                            'join_field'          =>  array('id'),
                            'object_field_name'   =>  array('post_id'),
                            'compare_field_table' =>  'id',
                            'compare_field_join'  =>  'topics_id');
    */

    $this->_init($init, $where);
  }
  function genSort(){
    $sort  = FormUtil::getPassedValue ('order', false);
    $result = false;
    switch ($sort){
      case 'id':
        $result = 'ORDER BY tpc_obj_status,tpc_id DESC';
        break;
      case 'type':
        $result = 'ORDER BY tpc_obj_status,tpc_type_id';
        break;
      case 'category':
        $result = 'ORDER BY tpc_obj_status,tpc_category_id';
        break;
      case 'subcategory':
        $result = 'ORDER BY tpc_obj_status,tpc_subcategory_id';
        break;
      case 'title':
        $result = 'ORDER BY tpc_obj_status,tpc_title';
        break;
      case 'date':
        $result = 'ORDER BY tpc_obj_status,tpc_cr_date DESC';
        break;
      case 'view':
        $result = 'ORDER BY tpc_count_view DESC';
        break;
      case 'post':
        $result = 'ORDER BY count_post DESC';
        break;
    }
    if ($result){
      return $result;
    }else{
      //default order
      return 'ORDER BY tpc_obj_status,tpc_cr_date DESC';
    }
  
  }

  function genFilter(){
    $filter  = FormUtil::getPassedValue ('filter', false);
    $keyword = DataUtil::formatForStore($filter['keyword']);
    $type = DataUtil::formatForStore($filter['type']);

    $category_id = DataUtil::formatForStore(FormUtil::getPassedValue ('category_id', $filter['category_id']));
    $subcategory_id = DataUtil::formatForStore(FormUtil::getPassedValue ('subcategory_id', $filter['subcategory_id']));
    $type_id = DataUtil::formatForStore($filter['type_id']);

    $wheres = array();
    //filter by user_id
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    if ($user_id) $wheres[] = "tpc_cr_uid = '$user_id'";
    //filter by keyword
    if ($keyword && $type){
      switch($type){
        case 'id': $wheres[] = "tpc_ref_id LIKE '%". $keyword ."%'"; break;
        case 'name': $wheres[] = "tpc_name LIKE '%". $keyword ."%'"; break;
        case 'brand': $wheres[] = "tpc_car_brand LIKE '%". $keyword ."%'"; break;
        case 'title': $wheres[] = "tpc_title LIKE '%". $keyword ."%'"; break;
        case 'category_name': $wheres[] = "cty_name LIKE '%". $keyword ."%'"; break;
        case 'subcategory_name': $wheres[] = "scy_name LIKE '%". $keyword ."%'"; break;
      }
    }
    //filter by selector
    if ($category_id && intval($category_id)) $wheres[] = "tpc_category_id ='$category_id'";
    if ($subcategory_id && intval($subcategory_id)) $wheres[] = "tpc_subcategory_id ='$subcategory_id'";
    if ($type_id && intval($type_id)) $wheres[] = "tpc_type_id ='$type_id'";
  
    //not display admin
    $pagetype = InnoUtil::getPageType();
    if ($pagetype == 'home'){
      $admin_id = constant('ADMIN_UID');
      $wheres[] = "(tpc_cr_uid <> '$admin_id' OR tpc_obj_status = '1')";
    }

    return implode(' AND ', $wheres) ;
  }

  function selectExtendResult(){
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    $result = array();
    //get user info
    if ($user_id){
      $result['user'] = InnoUtil::getUserInfo($user_id);
    }
    $category_id = DataUtil::formatForStore(FormUtil::getPassedValue ('category_id', false));
    $result['category'] = DBUtil::selectObjectArray ('innoforum_category');
    if($category_id){
      $result['category_name'] = DBUtil::selectFieldByID ('innoforum_category', 'name', $category_id);
      $result['subcategory'] = DBUtil::selectObjectArray ('innoforum_subcategory', "WHERE scy_category_id = '$category_id' ");
    }
/*
    $result['fimage'] = DBUtil::selectObject('innoforum_resource_image',
                                                   "WHERE rsi_referer_id = $this->_objData['id'] and rsi_status = 1",
                                                    "rsi_referer_id",
                                                    -1,
                                                    1,
                                                    '',
                                                    null,
                                                    array("filename","thumbname")
                                                  );
*/
    return $result;
  }
/*
  function selectPostProcess(){
    $id = $this->_objData['id'];

    $result['fimage'] = DBUtil::selectObjectArray('innoforum_resource_image',
                                                   "WHERE rsi_referer_id = ".$this->_objData['id']." and rsi_status = 1",
                                                    "rsi_id",
                                                    -1,
                                                    1,
                                                    '',
                                                    null,
                                                    array("filename","thumbname")
                                                  );

    //return $id;
  }
*/

}